<?php
namespace app\admin\controller;
use think\Controller;
use think\Request;
use catetree\Catetree;
class Cate extends Controller
{
	public function lst()
	{	
		//栏目的排序
		$cate=new Catetree();
		$cateObj=db('cate');
		if(request()->isPost())
		{
			$data=input('post.');
			$result=$cate->cateSort($data['sort'],$cateObj);
			if($result)
			{
				$this->success('排序成功',url('lst'));
			}else
			{
				$this->error('排序失败');
			}

		}
		$cateRes=$cateObj->order('sort desc')->select();
		$cateRes=$cate->catetree($cateRes);
		$settitle=settitle('分类');
		$this->assign(
			array(
				'settitle'=>$settitle,
				'cateRes'=>$cateRes
			)
		);
		return view();
	}
	public function add()
	{		
		$cate=new Catetree();
		$cateObj=db('cate');
		if(request()->isPost())
		{
			$data=input('post.');
			//验证
    		$validate = validate('Cate');
    		if(!$validate->check($data)){
			    $this->error($validate->getError());
			}

			if($data['pid']==1)
			{
				$this->error('系统分类不能再添加子栏目');
			}

			if($data['pid']==3)
			{
				$this->error('网店信息分类不能再添加子栏目');
			}

			if($data['pid']==2)
			{
				$data['cate_type']=3;
			}
			$c_pid=$cateObj->field('pid')->find($data['pid']);
			$cpid=$c_pid['pid'];
			if($cpid==2)
			{
				$this->error('网店帮助不能再添加子栏目');
			}
			$add=$cateObj->insert($data);
			if($add)
			{
				$this->success('添加分类成功',url('lst'));
			}else
			{
				$this->error('添加分类失败');
			}
		}
		$settitle=settitle('分类');
		$cateRes=$cateObj->order('sort desc')->select();
		$cateRes=$cate->catetree($cateRes);
		$this->assign(
			array(
				'settitle'=>$settitle,
				'cateRes'=>$cateRes
			)
		);
		return view();
	}

	public function edit()
	{	
		$id=input('id');
		$cate=new Catetree();
		$cateObj=db('cate');
		if(request()->isPost())
		{
			$data=input('post.');

			if(empty($data['pid']))
			{
				$cate_pid=$cateObj->where('id',$data['id'])->field('pid')->find();
				$cate_pid=$cate_pid['pid'];
				$data['pid']=$cate_pid;
			}	
			//验证
    		$validate = validate('Cate');
    		if(!$validate->check($data)){
			    $this->error($validate->getError());
			}

			if($data['pid']==1)
			{
				$this->error('系统分类不能再添加子栏目');
			}

			if($data['pid']==3)
			{
				$this->error('网店信息分类不能再添加子栏目');
			}

			if($data['pid']==2)
			{
				$data['cate_type']=3;
			}
			$c_pid=$cateObj->field('pid')->find($data['pid']);
			$cpid=$c_pid['pid'];
			if($cpid==2)
			{
				$this->error('网店帮助不能再添加子栏目');
			}
			$save=$cateObj->update($data);
			if($save!=false)
			{
				$this->success('修改分类成功',url('lst'));
			}else
			{
				$this->error('修改分类失败');
			}
		}
		$cates=$cateObj->find($id);
		$settitle=settitle('分类');
		$cateRes=$cateObj->order('sort desc')->select();
		$cateRes=$cate->catetree($cateRes);
		$this->assign(
			array(
				'cates'=>$cates,
				'settitle'=>$settitle,
				'cateRes'=>$cateRes
			)
		);
		return view();
	}

	public function del($id)
	{	
		//先查询当前分类的子分类
		$cate=db('cate');
		$cateTree=new Catetree();
		$sonids=$cateTree->childrenids($id,$cate);
		$sonids[]=intval($id);

		//系统内置分类不能删除
		$arrSys=[1,2,3];
		$arrRes=array_intersect($sonids,$arrSys);
		if(!empty($arrRes))
		{
			$this->error('系统内置分类不可删除');
		}
		//删除分类前判断该分类下的文章和文章相关缩略图
        $article=db('article');
        foreach ($sonids as $k => $v) {
            $artRes=$article->field('id,thumb')->where(array('cat_id'=>$v))->select();
            foreach ($artRes as $k1 => $v1) {
            	$fileurl=ROOT_PATH.'public'.DS.'static'.DS.'index'.DS.'image'.DS.'article';
				$thumbSrc=$fileurl.'\\'.$v1['thumb'];
                if(file_exists($thumbSrc)){
                    @unlink($thumbSrc);
                }
                $article->delete($v1['id']);
            }
        }
		$del=$cate->delete($sonids);
		if($del)
		{
			$this->success('删除分类成功',url('lst'));
		}else
		{
			$this->error('删除分类失败');
		}
	}





}
